package com.fage.spring.impl;

import com.fage.pojo.Account;
import com.fage.spring.IAccountDao;
import com.fage.spring.JdbcDaoSupport;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author lin
 * @version 1.0
 * @date 2020-08-11 16:04
 * @Description TODO
 */
/**
 * 账户的持久层实现类
 */
@Repository("accountDao")
public class AccountDaoImpl  extends JdbcDaoSupport implements IAccountDao {


    @Override
    public Account findAccountById(Integer accountId) {
        List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
        //super.getJdbcTemplate()方法是从父类上继承下来的，直接使用getJdbcTemplate()也一样。
        return accounts.isEmpty()?null:accounts.get(0);
    }

    @Override
    public Account findAccountByName(String accountName) {
        List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
        if(accounts.isEmpty()){
            return null;
        }
        if(accounts.size()>1){
            throw new RuntimeException("结果集不唯一");
        }
        return accounts.get(0);
    }

    @Override
    public void updateAccount(Account account) {
        super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
    }

    @Override
    protected void checkDaoConfig() throws IllegalArgumentException {

    }

    @Override
    protected void initTemplateConfig() {

    }
}